package c.e.b.h.e.s;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import c.e.a.b.g.f;
import c.e.a.b.g.h;
import c.e.b.h.e.k.l0;
import c.e.b.h.e.k.x0;
import c.e.b.h.e.s.i.b;
import c.e.b.h.e.s.i.e;
import c.e.b.h.e.s.i.g;
import c.e.b.h.e.s.j.c;
import java.io.Closeable;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: SettingsController */
public class d implements e {
    public final Context a;
    public final g b;
    public final f c;
    public final x0 d;
    public final a e;
    public final c.e.b.h.e.s.j.d f;
    public final l0 g;
    public final AtomicReference<e> h;
    public final AtomicReference<h<b>> i = new AtomicReference(new h());

    /* compiled from: SettingsController */
    public class a implements f<Void, Void> {
        public c.e.a.b.g.g a(Object obj) throws Exception {
            JSONObject g;
            Throwable e;
            d dVar;
            String str;
            Editor edit;
            h hVar;
            Throwable th;
            Void voidR = (Void) obj;
            String str2 = "FirebaseCrashlytics";
            d dVar2 = d.this;
            c.e.b.h.e.s.j.d dVar3 = dVar2.f;
            g gVar = dVar2.b;
            c cVar = (c) dVar3;
            Objects.requireNonNull(cVar);
            Closeable closeable = null;
            try {
                Map f = cVar.f(gVar);
                c.e.b.h.e.n.b c = cVar.c(f);
                cVar.d(c, gVar);
                c.e.b.h.e.b bVar = cVar.f;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("Requesting settings from ");
                stringBuilder.append(cVar.a);
                bVar.b(stringBuilder.toString());
                bVar = cVar.f;
                stringBuilder = new StringBuilder();
                stringBuilder.append("Settings query params were: ");
                stringBuilder.append(f);
                bVar.b(stringBuilder.toString());
                c.e.b.h.e.n.d a = c.a();
                c.e.b.h.e.b bVar2 = cVar.f;
                StringBuilder stringBuilder2 = new StringBuilder();
                stringBuilder2.append("Settings request ID: ");
                stringBuilder2.append(a.c.c("X-REQUEST-ID"));
                bVar2.b(stringBuilder2.toString());
                g = cVar.g(a);
            } catch (IOException e2) {
                if (cVar.f.a(6)) {
                    Log.e(str2, "Settings request failed.", e2);
                }
                g = null;
            }
            if (g != null) {
                Closeable fileWriter;
                c.e.b.h.e.s.i.f a2 = d.this.c.a(g);
                a aVar = d.this.e;
                long j = a2.d;
                Objects.requireNonNull(aVar);
                c.e.b.h.e.b bVar3 = c.e.b.h.e.b.a;
                String str3 = "Failed to close settings writer.";
                bVar3.b("Writing settings to cache file...");
                try {
                    g.put("expires_at", j);
                    fileWriter = new FileWriter(aVar.a());
                    try {
                        fileWriter.write(g.toString());
                        fileWriter.flush();
                    } catch (Exception e3) {
                        e = e3;
                    }
                } catch (Exception e4) {
                    e = e4;
                    fileWriter = null;
                    try {
                        String str4 = "Failed to cache settings";
                        try {
                            if (bVar3.a(6)) {
                                Log.e(str2, str4, e);
                            }
                            c.e.b.h.e.k.g.c(fileWriter, str3);
                            d.this.e(g, "Loaded settings: ");
                            dVar = d.this;
                            str = dVar.b.f;
                            edit = c.e.b.h.e.k.g.n(dVar.a).edit();
                            edit.putString("existing_instance_identifier", str);
                            edit.apply();
                            d.this.h.set(a2);
                            ((h) d.this.i.get()).b(a2.a);
                            hVar = new h();
                            hVar.b(a2.a);
                            d.this.i.set(hVar);
                            return c.e.a.b.b.j.d.b(null);
                        } catch (Throwable th2) {
                            th = th2;
                            c.e.b.h.e.k.g.c(fileWriter, str3);
                            throw th;
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        closeable = fileWriter;
                        fileWriter = closeable;
                        c.e.b.h.e.k.g.c(fileWriter, str3);
                        throw th;
                    }
                } catch (Throwable th4) {
                    th = th4;
                    fileWriter = closeable;
                    c.e.b.h.e.k.g.c(fileWriter, str3);
                    throw th;
                }
                c.e.b.h.e.k.g.c(fileWriter, str3);
                d.this.e(g, "Loaded settings: ");
                dVar = d.this;
                str = dVar.b.f;
                edit = c.e.b.h.e.k.g.n(dVar.a).edit();
                edit.putString("existing_instance_identifier", str);
                edit.apply();
                d.this.h.set(a2);
                ((h) d.this.i.get()).b(a2.a);
                hVar = new h();
                hVar.b(a2.a);
                d.this.i.set(hVar);
            }
            return c.e.a.b.b.j.d.b(null);
        }
    }

    public d(Context context, g gVar, x0 x0Var, f fVar, a aVar, c.e.b.h.e.s.j.d dVar, l0 l0Var) {
        x0 x0Var2 = x0Var;
        AtomicReference atomicReference = new AtomicReference();
        this.h = atomicReference;
        this.a = context;
        this.b = gVar;
        this.d = x0Var2;
        this.c = fVar;
        this.e = aVar;
        this.f = dVar;
        this.g = l0Var;
        JSONObject jSONObject = new JSONObject();
        atomicReference.set(new c.e.b.h.e.s.i.f(b.b(x0Var2, 3600, jSONObject), null, new c.e.b.h.e.s.i.d(jSONObject.optInt("max_custom_exception_events", 8), 4), new c.e.b.h.e.s.i.c(jSONObject.optBoolean("collect_reports", true)), 0, 3600));
    }

    public c.e.a.b.g.g<b> a() {
        return ((h) this.i.get()).a;
    }

    /* JADX WARNING: Removed duplicated region for block: B:36:? A:{SYNTHETIC, RETURN} */
    /* JADX WARNING: Removed duplicated region for block: B:30:0x006a  */
    public final c.e.b.h.e.s.i.f b(c.e.b.h.e.s.c r11) {
        /*
        r10 = this;
        r0 = "FirebaseCrashlytics";
        r1 = c.e.b.h.e.b.a;
        r2 = 6;
        r3 = 0;
        r4 = c.e.b.h.e.s.c.SKIP_CACHE_LOOKUP;	 Catch:{ Exception -> 0x0046 }
        r4 = r4.equals(r11);	 Catch:{ Exception -> 0x0046 }
        if (r4 != 0) goto L_0x006f;
    L_0x000e:
        r4 = r10.e;	 Catch:{ Exception -> 0x0046 }
        r4 = r4.b();	 Catch:{ Exception -> 0x0046 }
        if (r4 == 0) goto L_0x005e;
    L_0x0016:
        r5 = r10.c;	 Catch:{ Exception -> 0x0046 }
        r5 = r5.a(r4);	 Catch:{ Exception -> 0x0046 }
        if (r5 == 0) goto L_0x0052;
    L_0x001e:
        r6 = "Loaded cached settings: ";
        r10.e(r4, r6);	 Catch:{ Exception -> 0x0046 }
        r4 = r10.d;	 Catch:{ Exception -> 0x0046 }
        java.util.Objects.requireNonNull(r4);	 Catch:{ Exception -> 0x0046 }
        r6 = java.lang.System.currentTimeMillis();	 Catch:{ Exception -> 0x0046 }
        r4 = c.e.b.h.e.s.c.IGNORE_CACHE_EXPIRATION;	 Catch:{ Exception -> 0x0046 }
        r11 = r4.equals(r11);	 Catch:{ Exception -> 0x0046 }
        if (r11 != 0) goto L_0x0048;
    L_0x0034:
        r8 = r5.d;	 Catch:{ Exception -> 0x0046 }
        r11 = (r8 > r6 ? 1 : (r8 == r6 ? 0 : -1));
        if (r11 >= 0) goto L_0x003c;
    L_0x003a:
        r11 = 1;
        goto L_0x003d;
    L_0x003c:
        r11 = 0;
    L_0x003d:
        if (r11 != 0) goto L_0x0040;
    L_0x003f:
        goto L_0x0048;
    L_0x0040:
        r11 = "Cached settings have expired.";
        r1.b(r11);	 Catch:{ Exception -> 0x0046 }
        goto L_0x006f;
    L_0x0046:
        r11 = move-exception;
        goto L_0x0064;
    L_0x0048:
        r11 = "Returning cached settings.";
        r1.b(r11);	 Catch:{ Exception -> 0x004f }
        r3 = r5;
        goto L_0x006f;
    L_0x004f:
        r11 = move-exception;
        r3 = r5;
        goto L_0x0064;
    L_0x0052:
        r11 = "Failed to parse cached settings data.";
        r4 = r1.a(r2);	 Catch:{ Exception -> 0x0046 }
        if (r4 == 0) goto L_0x006f;
    L_0x005a:
        android.util.Log.e(r0, r11, r3);	 Catch:{ Exception -> 0x0046 }
        goto L_0x006f;
    L_0x005e:
        r11 = "No cached settings data found.";
        r1.b(r11);	 Catch:{ Exception -> 0x0046 }
        goto L_0x006f;
    L_0x0064:
        r1 = r1.a(r2);
        if (r1 == 0) goto L_0x006f;
    L_0x006a:
        r1 = "Failed to get cached settings";
        android.util.Log.e(r0, r1, r11);
    L_0x006f:
        return r3;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.b.h.e.s.d.b(c.e.b.h.e.s.c):c.e.b.h.e.s.i.f");
    }

    public e c() {
        return (e) this.h.get();
    }

    public c.e.a.b.g.g<Void> d(c cVar, Executor executor) {
        c.e.b.h.e.s.i.f b;
        if ((c.e.b.h.e.k.g.n(this.a).getString("existing_instance_identifier", "").equals(this.b.f) ^ 1) == 0) {
            b = b(cVar);
            if (b != null) {
                this.h.set(b);
                ((h) this.i.get()).b(b.a);
                return c.e.a.b.b.j.d.b(null);
            }
        }
        b = b(c.IGNORE_CACHE_EXPIRATION);
        if (b != null) {
            this.h.set(b);
            ((h) this.i.get()).b(b.a);
        }
        return this.g.b().l(executor, new a());
    }

    public final void e(JSONObject jSONObject, String str) throws JSONException {
        c.e.b.h.e.b bVar = c.e.b.h.e.b.a;
        StringBuilder g = c.b.a.a.a.g(str);
        g.append(jSONObject.toString());
        bVar.b(g.toString());
    }
}
